﻿Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Data.Linq
Imports System.Text
Imports DevComponents
Imports System.Data
Imports System.Text.RegularExpressions
Imports TNCDAL
Imports HTLibrary
Public Class ucOilfuel

    Dim moil As New MOilPrice
    Dim util As New Utilities
    Dim munitprice As New MOilUnitPrices
    Dim col1 As DataGridViewTextBoxColumn
    Dim col2 As DataGridViewComboBoxColumn
    Dim col3 As DevComponents.DotNetBar.Controls.DataGridViewDateTimeInputColumn
    Dim TotRowCount As Integer
    Private NewTruckID As Integer
    Private isNewOilExpense As Boolean
    Private isEditOilExpese As Boolean
    Dim blnLocthang As Boolean = False
    Dim blnLocquy As Boolean = False
    Dim blnLocnam As Boolean = False
    Dim filterTab1 As Boolean = False
    Dim blnLocthang2 As Boolean = False
    Dim blnLocquy2 As Boolean = False
    Dim blnLocnam2 As Boolean = False
    Dim blnLocSoxe2 As Boolean = False
    Dim blnLocTaixe2 As Boolean = False
    Dim filterTab2 As Boolean = False
    Dim _isNewOilPayment As Boolean = False
    Dim _isEditOilPayment As Boolean = False
    Dim isLoadcbMonth As Boolean = False
    Dim isLoadcbYear As Boolean = False
    Dim moilPay As New MOilPayments

    Dim TotRowCountOidPayment As Integer
    Dim RowId As Integer
    Dim editRowID As Integer = 0
    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()
        DataGridViewHelper.DataGridView_InitEvent(gridOilPayment)
        ' Add any initialization after the InitializeComponent() call.

    End Sub
    Sub AddFilterTab1()
        cbLocThang1.SelectedIndex = 0
        cbLocQuy1.SelectedIndex = 0
        Dim year As Integer = DateTime.Now.Year
        cbLocNam1.Items.Add(New DevComponents.Editors.ComboItem("-------"))
        For i As Integer = 0 To 5 Step 1
            cbLocNam1.Items.Add(New DevComponents.Editors.ComboItem(year - i))
        Next
        cbLocNam1.SelectedIndex = 0
    End Sub

   
    Private Sub FilterTab1Fn()

        'Lọc theo tháng, quý và năm


        If cbLocThang1.SelectedIndex <> 0 Then
            blnLocthang = True
        Else : blnLocthang = False
        End If

        If cbLocQuy1.SelectedIndex <> 0 Then
            blnLocquy = True
        Else : blnLocquy = False
        End If

        If cbLocNam1.SelectedIndex <> 0 Then
            blnLocnam = True
        Else : blnLocnam = False
        End If

        If cbLocQuy1.SelectedIndex <> 0 And cbLocThang1.SelectedIndex <> 0 Then
            'Hỏi Lọc theo quý hay tháng
            MessageBox.Show("Chỉ chọn lọc theo quý hoặc tháng")
            Return
        End If

        'nếu lọc theo tháng và năm

        If blnLocthang And Not blnLocquy And blnLocnam Then
            'Nếu chỉ chọn tháng
            Dim cbItem1 As DevComponents.Editors.ComboItem = CType(cbLocThang1.SelectedItem, DevComponents.Editors.ComboItem)
            Dim cbItem2 As DevComponents.Editors.ComboItem = CType(cbLocNam1.SelectedItem, DevComponents.Editors.ComboItem)

            gridOilPayment.DataSource = moilPay.GetPaymentList2(cbItem1.Text, 0, cbItem2.Text)
        End If

        'nếu lọc theo quý và năm
        If Not blnLocthang And blnLocquy And blnLocnam Then
            'Nếu chỉ chọn tháng
            Dim cbItem1 As DevComponents.Editors.ComboItem = CType(cbLocQuy1.SelectedItem, DevComponents.Editors.ComboItem)
            Dim cbItem2 As DevComponents.Editors.ComboItem = CType(cbLocNam1.SelectedItem, DevComponents.Editors.ComboItem)

            gridOilPayment.DataSource = moilPay.GetPaymentList2(0, cbItem1.Text, cbItem2.Text)
        End If
        If blnLocthang And Not blnLocquy And Not blnLocnam Then
            'Nếu chỉ chọn tháng
            Dim cbItem As DevComponents.Editors.ComboItem = CType(cbLocThang1.SelectedItem, DevComponents.Editors.ComboItem)
            gridOilPayment.DataSource = moilPay.GetPaymentList2(cbItem.Text, 0, 0)
        End If

        If Not blnLocthang And blnLocquy And Not blnLocnam Then
            'Nếu lọc quý
            Dim cbItem As DevComponents.Editors.ComboItem = CType(cbLocQuy1.SelectedItem, DevComponents.Editors.ComboItem)
            gridOilPayment.DataSource = moilPay.GetPaymentList2(0, cbItem.Text, 0)
        End If
        If Not blnLocthang And Not blnLocquy And blnLocnam Then
            'Nếu lọc năm
            Dim cbItem As DevComponents.Editors.ComboItem = CType(cbLocNam1.SelectedItem, DevComponents.Editors.ComboItem)
            gridOilPayment.DataSource = moilPay.GetPaymentList2(0, 0, cbItem.Text)
        End If
    End Sub

    Private Sub ucOilfuel_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        gridOilPayment.EditMode = DataGridViewEditMode.EditOnEnter
        ' add column for payment method
        AddFilterTab1()


        BindingTogridOilPayment()
        FormatGrid()
        isNewOilExpense = False
    End Sub

    Private Sub FormatGrid()
        gridOilPayment.Columns(1).DefaultCellStyle.Format = "#,###"
        gridOilPayment.Columns(1).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        gridOilPayment.Columns(3).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        gridOilPayment.Columns(3).DefaultCellStyle.Format = "dd/MM/yyyy"

    End Sub

    Sub EnableOilPaymentTab(ByVal value As Boolean)
        txtAmountOilPayment.Enabled = value
        radTKCN.Checked = True
        dateOilPayment.Enabled = value
        txtDescriptionOilPayment.Enabled = value


        If _isNewOilPayment Then
            'Neu them moi
            txtIDOilPayment.Text = ""
            txtAmountOilPayment.Text = ""
            dateOilPayment.Value = DateTime.Now
            txtDescriptionOilPayment.Text = ""
            txtAmountOilPayment.Focus()
        End If

        If _isEditOilPayment Or _isNewOilPayment Then
            btnSavePayment.Enabled = value
        End If

    End Sub

    Sub BindingTogridOilPayment()
        EnableOilPaymentTab(False)
        If filterTab1 Then
            'Nếu đang trong tình trạng lọc dữ liệu
            FilterTab1Fn()
        Else
            Dim moil As New MOilPayments
            Dim query = moil.GetPaymentList2()
            Dim source As New BindingSource
            source.DataSource = query
            BindingNavigator1.BindingSource = source

            gridOilPayment.DataSource = source
        End If


        gridOilPayment.Columns(0).ReadOnly = True
        TotRowCountOidPayment = gridOilPayment.RowCount - 1
        gridOilPayment.AllowUserToAddRows = False

        gridOilPayment.Columns(3).DefaultCellStyle.Format = "dd'/'MM'/'yyyy"
        Try
            gridOilPayment.CurrentCell.Selected = False
        Catch ex As Exception

        End Try
    End Sub

    Private Sub btnSavePayment_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSavePayment.Click

        Dim obj As New tblOilPayment

        If txtAmountOilPayment.Text = "" Then
            'Nếu chưa nhập số tiền 
            MessageBox.Show("Nhập số tiền")
            Return
        End If
        obj.Amount = Integer.Parse(txtAmountOilPayment.Text.Replace(",", ""))

        If radTKCN.Checked Then
            obj.Method = "TKCN"
        End If

        If radCK.Checked Then
            obj.Method = "C/K"
        End If

        obj.DatePayment = Convert.ToDateTime(dateOilPayment.Value.ToString("MMMM dd, yyyy"))
        obj.Description = txtDescriptionOilPayment.Text
        If _isNewOilPayment Then
            'Nếu là thêm mới           
         
            moilPay.InsertNewRecord(obj)
            BindingTogridOilPayment()
            gridOilPayment.Rows(editRowID).Selected = True
            gridOilPayment.FirstDisplayedScrollingRowIndex = editRowID
            gridOilPayment.CurrentCell = gridOilPayment(1, editRowID)
        End If

        If _isEditOilPayment Then
            'Nếu là chỉnh sửa
            _isNewOilPayment = False
            _isEditOilPayment = False
            obj.ID_OilPayment = CInt(txtIDOilPayment.Text)
            moilPay.UpdateRecord(obj)

            BindingTogridOilPayment()
            gridOilPayment.Rows(editRowID).Selected = True
            gridOilPayment.FirstDisplayedScrollingRowIndex = editRowID
            gridOilPayment.CurrentCell = gridOilPayment(1, editRowID)
        End If


        ' btnDelete.Visible = False
        'btnInsert.Visible = False
        '  btnDelete.Visible = False
        btnAdd.Enabled = True
        btnEdit.Enabled = True
        btnDelPayment.Enabled = True
        btnSavePayment.Enabled = False
        btnCancelPayment.Enabled = False
        gridOilPayment.Enabled = True
    End Sub

    Private Sub btnDelPayment_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelPayment.Click
        If (MessageBox.Show("Do you really want to delete selected record?",
                     "Delete Warning", MessageBoxButtons.YesNo,
                     MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2, 0, False) = DialogResult.Yes) Then

            'For i As Integer = 0 To gridOilPayment.SelectedRows.Count - 1
            '    RowId = gridOilPayment.SelectedRows(i).Index
            '    eoilPay.id = Convert.ToInt32(gridOilPayment(0, RowId).Value)

            '    If eoilPay.id <> 0 Then
            '        moilPay.DeleteRecord(eoilPay.id)
            '    End If

            'Next

            moilPay.DeleteRecord(CInt(txtIDOilPayment.Text))
        End If

         
        If filterTab1 Then
            FilterTab1Fn()
        Else
            BindingTogridOilPayment()
        End If
        btnAdd.Enabled = True
        btnEdit.Enabled = True
        btnDelPayment.Enabled = True
        btnSavePayment.Enabled = False
        btnCancelPayment.Enabled = False
        ' btnDelete.Visible = False
        ' btnInsert.Visible = False
        ' btnEdit.Visible = False
    End Sub

    Private Sub btnInsertOilPayment_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        'If gridOilPayment.Rows(index).Cells(2).FormattedValue.ToString() <> "" Then
        '    eoilPay.method = gridOilPayment.Rows(index).Cells(2).Value.ToString()
        'Else : eoilPay.method = vbNull
        'End If

        'eoilPay.amount = Convert.ToInt32(gridOilPayment.Rows(index).Cells(1).Value)
        'eoilPay.id = Convert.ToInt32(gridOilPayment.Rows(index).Cells(0).Value)
        'If gridOilPayment.Rows(index).Cells(3).FormattedValue.ToString() <> "" Then
        '    eoilPay.datepayment = Convert.ToDateTime(gridOilPayment.Rows(index).Cells(3).Value.ToString())
        'Else : eoilPay.datepayment = ""
        'End If
        'Try
        '    Dim obj As New tblOilPayment

        '    If Not IsDBNull(eoilPay.amount) And Not IsDBNull(eoilPay.datepayment) Then
        '        obj.ID_OilPayment = eoilPay.id
        '        obj.Amount = eoilPay.amount
        '        obj.Method = eoilPay.method
        '        obj.DatePayment = eoilPay.datepayment

        '        moilPay.UpdateRecord(obj)
        '        'mcur.InsertNewRecord(objCurr)



        '        moilPay.InsertNewRecord(obj)
        '        bindingTogrid()
        '    End If


        'Catch ex As Exception
        '    Throw ex
        'End Try
    End Sub

    

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        _isNewOilPayment = True
        _isEditOilPayment = False
        EnableOilPaymentTab(True)
        btnEdit.Enabled = False
        btnAdd.Enabled = False
        btnDelPayment.Enabled = False
        btnSavePayment.Enabled = True
        btnCancelPayment.Enabled = True
        txtAmountOilPayment.Focus()
        gridOilPayment.Enabled = False
    End Sub

    Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
        _isEditOilPayment = True
        _isNewOilPayment = False
        EnableOilPaymentTab(True)
        btnAdd.Enabled = False
        btnDelPayment.Enabled = False
        btnSavePayment.Enabled = True
        btnCancelPayment.Enabled = True
        btnEdit.Enabled = False
        gridOilPayment.Enabled = False
        editRowID = gridOilPayment.CurrentCell.RowIndex
        txtAmountOilPayment.Focus()
    End Sub
    
  

    Private Sub btnLoc1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoc1.Click
        'Nút lọc thông tin chi trả tiền xăng dầu cho hãng
        filterTab1 = True
        FilterTab1Fn()
    End Sub

 

   

    Private Sub btnCancelPayment_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancelPayment.Click
        _isEditOilPayment = False
        _isNewOilPayment = False
        EnableOilPaymentTab(False)
        BindingTogridOilPayment()
        btnAdd.Enabled = True
        btnEdit.Enabled = True
        btnDelPayment.Enabled = True
        btnSavePayment.Enabled = False
        btnCancelPayment.Enabled = False
        editRowID = 0
        gridOilPayment.Enabled = True
    End Sub

    Private Sub btnBoLoc1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBoLoc1.Click
        filterTab1 = False
        BindingTogridOilPayment()
    End Sub

    Private Sub gridOilPayment_RowEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles gridOilPayment.RowEnter
        Dim rw As Integer = e.RowIndex
        Dim dg As DataGridView = CType(sender, DataGridView)
        txtIDOilPayment.Text = dg.Item(0, rw).Value   'row.Item("id").ToString
        If dg.Item(1, rw).Value IsNot Nothing Then

            txtAmountOilPayment.Text = CInt(dg.Item(1, rw).Value)
        End If


        If dg.Item(2, rw).Value IsNot Nothing Then


            If dg.Item(2, rw).Value.ToString() = "TKCN" Then
                radTKCN.Checked = True
            End If

            If dg.Item(2, rw).Value.ToString() = "C/K" Then
                radCK.Checked = True

            End If


        End If
        If dg.Item(3, rw).Value IsNot Nothing Then

            dateOilPayment.Value = dg.Item(3, rw).Value
        End If
        txtDescriptionOilPayment.Text = dg.Item(4, rw).Value
    End Sub

    Private Sub btnBoLoc2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        'Bỏ  lọc tab chi tiết mua xăng dầu

    End Sub

    
    Private Sub gridOilManager_DataBindingComplete(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewBindingCompleteEventArgs)
        Dim gridView As DataGridView = CType(sender, DataGridView)

        If gridView IsNot Nothing Then
            ' sum = 0
            For Each r As DataGridViewRow In gridView.Rows

                gridView.Rows(r.Index).HeaderCell.Value = (r.Index + 1).ToString()
                '  sum += CDbl(gridView.Rows(r.Index).Cells(1).Value)
            Next

        End If
    End Sub

    Private Sub btnLoc2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        'Nút lọc thông tin chi trả tiền xăng dầu cho hãng
        filterTab2 = True

    End Sub

   

   
    Private Sub gridTotalMonth_DataBindingComplete(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewBindingCompleteEventArgs)
        Dim gridView As DataGridView = CType(sender, DataGridView)

        If gridView IsNot Nothing Then
            For Each r As DataGridViewRow In gridView.Rows
                gridView.Rows(r.Index).HeaderCell.Value = (r.Index + 1).ToString()
            Next

        End If

    End Sub

   
    
    
    Private Sub txtAmountOilPayment_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtAmountOilPayment.TextChanged
        util.FormatCurr(txtAmountOilPayment)
    End Sub

    Private Sub txtAmountOilPayment_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles txtAmountOilPayment.KeyPress
        If (Not Char.IsDigit(e.KeyChar) And Not Char.IsControl(e.KeyChar)) Then
            e.Handled = True
        End If
    End Sub
End Class
 